package yf.mall.controller.api.dy;

import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.web.bind.annotation.*;
import yf.mall.dysdk.service.DyAuthService;
import yf.mall.entity.DyAccountAuthEntity;
import yf.mall.service.app.DyAccountAuthService;
import yf.mall.service.app.DyUtilService;
import yf.mall.vo.app.dy.DyVideoVO;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/app/dy/util")
public class ApiAppDyUtilController {

    @Resource
    private DyUtilService dyUtilService;

    @Resource
    private DyAccountAuthService authService;

    @PostMapping("/video")
    public R video(@LoginUserId Long userId, @RequestParam String link, @RequestParam(required = false) Integer type) {
        List<DyAccountAuthEntity> auths = authService.getAllAuthByUser(userId);
        if (auths.isEmpty()) {
            return R.error("请先授权绑定抖音!");
        }
        DyVideoVO dyVideoVO = dyUtilService.parseVideo(link, type);
        return R.ok(dyVideoVO);
    }

    /**
     * 违禁词检测
     *
     * @return
     */
    @PostMapping("/text")
    public R text(@LoginUserId Long userId, @RequestParam String text) {
        List<DyAccountAuthEntity> auths = authService.getAllAuthByUser(userId);
        if (auths.isEmpty()) {
            return R.error("请先授权绑定抖音!");
        }
        Map res = dyUtilService.parseText(text);
        return R.ok(res);
    }

    /**
     * 暂时废弃
     *
     * @param link
     * @param request
     * @param response
     */
    @GetMapping("/video")
    @IgnoreAuth
    public void videoForward(@RequestParam String link, HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher(link).forward(request, response);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
